//
//  WQCMemberViewController.m
//  renxinghuan
//
//  Created by 王晓民 on 2018/3/23.
//  Copyright © 2018年 吴清翠. All rights reserved.
//

#import "WQCMemberViewController.h"
#import "configuration.h"
#import "vipModel.h"
#import <AlipaySDK/AlipaySDK.h>
#import "WQCUpateViewController.h"
@interface WQCMemberViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *memberTableView;
@property(nonatomic,strong)NSMutableArray *vipArr;
@end

@implementation WQCMemberViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadVip];
    self.vipArr = [[NSMutableArray alloc]init];
    self.view.backgroundColor =[UIColor blackColor];
    UIImageView  *bgView = [MyUIClass makeUIImageViewWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) andImage:@"bgview"];
    
    bgView.userInteractionEnabled =YES;
    [self.view addSubview:bgView];
    UILabel  *nameLb = [MyUIClass makeUILabelWithFrame:CGRectMake(0, kDevice_Is_iPhoneX?54:30, ScreenWidth, 20) andBackColor:[UIColor clearColor] andText:@"会员升级" andTextColor:[UIColor whiteColor] andFont:[UIFont systemFontOfSize:15] andAlignment:NSTextAlignmentCenter];
    [bgView addSubview:nameLb];
    UIButton  *btnBack = [MyUIClass makeUIButtonWithFrame:CGRectMake(10, kDevice_Is_iPhoneX?44:20, 40, 30) andType:UIButtonTypeCustom andTitle:@"" andImageName:@"Nav_back"andTarget:self andSelector:@selector(pressBack) andEvent:UIControlEventTouchUpInside andState:UIControlStateNormal];
    [bgView addSubview:btnBack];
    
    UILabel  *titLb = [MyUIClass makeUILabelWithFrame:CGRectMake(0, 300, ScreenWidth, 20) andBackColor:[UIColor clearColor] andText:@"-  会员权益  -" andTextColor:[UIColor blackColor] andFont:[UIFont systemFontOfSize:15] andAlignment:NSTextAlignmentCenter];
    [bgView addSubview:titLb];
    self.memberTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, titLb.y+20, kScreenW, kScreenH-220) style:UITableViewStyleGrouped];
    self.memberTableView.dataSource = self;
    self.memberTableView.delegate = self;
    self.memberTableView.backgroundColor = [UIColor clearColor];
    self.memberTableView.separatorColor = [UIColor clearColor];
    [self.view addSubview:self.memberTableView];

}
#pragma make ------->>>轮播图
- (void)loadVip{
    
    [HttpTool POST:vipList params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSLog(@"%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSArray * resultarray = responseObject[@"result"];
            for (NSDictionary * dict in resultarray) {
               
                
                vipModel * model = [vipModel mj_objectWithKeyValues:dict];
                [self.vipArr addObject:model];
            }
           
            
            for (int i=0; i<self.vipArr.count; i++) {
                
                vipModel *model = self.vipArr[i];
                if ([model.isMine isEqualToString:@"0"]) {
                    for (int j=0; j<=i;j++) {
                        vipModel *model = self.vipArr[j];
                        model.state = @"1";
                    }
                    
                    
                    
                }
                
                
            }
            
            
            
            [self.memberTableView reloadData];
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        //        [self showToast:Failed];
    }];
}
-(void)pressBack
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma make -------->>setNav

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark - TableView DataSource Method

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.vipArr.count-1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * strID = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:strID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strID];
    }
    
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    
   // vipModel *model = self.vipArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    vipModel  *model = self.vipArr[indexPath.row];
    UILabel *label= [MyUIClass makeUILabelWithFrame:CGRectMake(25, 10, 120, 20) andBackColor:[UIColor clearColor] andText:model.name andTextColor:[UIColor blackColor ] andFont:[UIFont boldSystemFontOfSize:15] andAlignment:NSTextAlignmentLeft];
    [cell.contentView addSubview:label];
    UILabel *timelabel= [MyUIClass makeUILabelWithFrame:CGRectMake(25, 30, 250, 60) andBackColor:[UIColor clearColor] andText:model.content andTextColor:[UIColor grayColor] andFont:[UIFont systemFontOfSize:13] andAlignment:NSTextAlignmentLeft];
    timelabel.numberOfLines = 0;
    [cell.contentView addSubview:timelabel];
    
    UIButton * loginButton = [[UIButton alloc] init];
    loginButton.frame = CGRectMake(ScreenWidth-100, 15, 70, 35);
    loginButton.backgroundColor = vipMain;
    loginButton.layer.cornerRadius = 15;
    loginButton.tag=indexPath.row;
    
  //  if (indexPath.row==0) {
        if ([model.state isEqualToString:@"1"]) {
             [loginButton setTitle:@"已开通" forState:UIControlStateNormal];
          
        }else{
              [loginButton setTitle:@"开通" forState:UIControlStateNormal];
        }
        
       
  //  }
//    if (indexPath.row==1) {
//        [loginButton setTitle:@"开通" forState:UIControlStateNormal];
//    }
    
    
    
    
    if (indexPath.row==2) {
        
       // NSLog(@"%@-%@",model.isMine,model.grade);
        
        
        if ([model.state isEqualToString:@"1"]) {
            [loginButton setTitle:@"已申请" forState:UIControlStateNormal];
            
        }else{
             [loginButton setTitle:@"申请" forState:UIControlStateNormal];
        }
      
        
        
       
    }
    
   
    loginButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(superBtn:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:loginButton];
    
    
    return cell;
}
-(void)superBtn:(UIButton *)button
{
    NSLog(@"第%ld个",(long)button.tag);

    if (button.tag == 2) {
        WQCUpateViewController  *updatView = [[WQCUpateViewController alloc]init];
        [self.navigationController pushViewController:updatView animated:YES];
    }
    else
    {
    vipModel  *model = self.vipArr[button.tag];
        if ([model.state isEqualToString:@"1"]) {
            NSLog(@"已开通");
            
        }else{
             [self loadzhifuVip:model.vipId];
        }
        
        
   
    }
}

-(void)loadzhifuVip:(NSString *)vipid
{
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"vipId"] = vipid;
    params[@"type"] = @"1";
    NSLog(@"开通%@",params);
    [HttpTool POST:openVip params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSLog(@"%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSString * result = responseObject[@"result"];
                [[AlipaySDK defaultService] payOrder:result fromScheme:@"renxinghuan" callback:^(NSDictionary *resultDic) {
                    NSLog(@"reslut = %@",resultDic);
                    NSString * memo = resultDic[@"memo"];
                    NSLog(@"===memo:%@", memo);
                    if ([resultDic[@"ResultStatus"] isEqualToString:@"9000"]) {
                        
                        [SVProgressHUD showSuccessWithStatus:@"支付成功"];
                        
                    }else{
                        [SVProgressHUD showImage:nil status:@"支付失败"];
                    }
                    
                }];
            
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
    }];
}


@end
